package joney.main.views.report;

import java.util.Date;
import java.util.Vector;

import joney.main.analysis.DateComparison;
import joney.model.Account;
import joney.model.DataModel;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;
import org.jfree.chart.JFreeChart;
import org.jfree.data.general.Dataset;
import org.jfree.experimental.chart.swt.ChartComposite;

public abstract class TimeRangeChart extends ViewPart {

	public static final String ALL = "All";
	
	protected Dataset dataset;
	private Composite chart;
	private boolean barOnBottom = false;
	
	private Combo yearDrop;
	
	public TimeRangeChart(boolean barOnBottom)
	{
		this.barOnBottom = barOnBottom;
	}
	
	public TimeRangeChart(){}
	
	public void createPartControl(Composite parent) {
		Composite mainSplit = new Composite(parent, SWT.None);
		mainSplit.setLayout(new GridLayout(1, false));
		
		dataset = createDataset(new Date().getYear()+1900, new Date().getYear()+1900);
		
		if(!barOnBottom){
			createAccountBar(createTimeBar(mainSplit));
		}
		
		chart = new ChartComposite(mainSplit , SWT.None, createChart(dataset));
		 
		//set layout for video frame
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.verticalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace= true;
		chart.setLayoutData(gridData);
		
		if(barOnBottom){
			createAccountBar(createTimeBar(mainSplit));
		}
		
	}
	
	private Composite createTimeBar(Composite mainSplit)
	{
		//set top panel layout
		Composite topPanel = new Composite(mainSplit, SWT.BORDER);
		GridData gridData = new GridData();
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessHorizontalSpace = true;
		topPanel.setLayoutData(gridData);
		topPanel.setLayout(new GridLayout(4, false));
		
		
		Vector<Date> be = DateComparison.getMaxBeginAndEndDates();
		
		int begYear, endYear;
		if(be.get(0) == null || be.get(1) == null)
		{
			begYear = 2010;
			endYear = begYear;
		}
		else
		{
			begYear =be.get(0).getYear()+1900;
			endYear = be.get(1).getYear()+1900;
		}
		
		Label year = new Label(topPanel, SWT.None);
		year.setText("Year:");
		
		yearDrop = new Combo(topPanel, SWT.DROP_DOWN);
		for(int i=begYear; i <= endYear;i++)
		{
			yearDrop.add(String.valueOf(i));
		}
		yearDrop.select(yearDrop.getItemCount()-1);
		yearDrop.add("All");
		
		yearDrop.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e) {
				
				if(yearDrop.getText().equals(ALL))
				{
					updateData(Integer.valueOf(yearDrop.getItem(0)), 
							Integer.valueOf(yearDrop.getItem(yearDrop.getItemCount()-2)));
				}
				else{
					int val = Integer.valueOf(yearDrop.getText());
					updateData(val, val);
				}
			}
		});
		
		return topPanel;
	}
	
	private void createAccountBar(Composite topPanel)
	{
		Label year = new Label(topPanel, SWT.None);
		year.setText("Account:");
		
		final Combo accountDrop = new Combo(topPanel, SWT.DROP_DOWN);
	
		for(Account a: DataModel.accounts)
		{
			accountDrop.add(a.getName());
		}
		accountDrop.select(accountDrop.getItemCount()-1);
		accountDrop.add("All");
		
		accountDrop.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e) {
				
				if(accountDrop.getText().equals(ALL))
				{
					updateData(Integer.valueOf(yearDrop.getItem(0)), 
							Integer.valueOf(yearDrop.getItem(yearDrop.getItemCount()-2)));
				}
				else{
					int val = Integer.valueOf(yearDrop.getText());
					updateData(yearDrop.getText(), val, val);
				}
			}
		});
	}
	
	protected abstract Dataset createDataset(int yearb, int yeare);
	protected abstract JFreeChart createChart(Dataset dataset); 
	protected abstract void updateData(int yearb, int yeare);
	protected abstract void updateData(String account, int yearb, int yeare);
	
	public void setFocus() {
		chart.setFocus();
	}

}
